﻿var CalculatorCar = function(scheduleCarElement, dateCarElement, quantityCarElement,soChoCarElement,concernCarElement,yearCarElement,priceCarElement,totalPriceCarElement,seasionCarElement,tourIdElement,vatCarElement,quantityAdultsElement,quantityChildElement) {
    //ham getPrice
    var fncPriceCar = function () {
        var priceCar = $(priceCarElement);
        var totalPriceCar = $(totalPriceCarElement);
        var typeId = 3;
        var scheduleCar = $(scheduleCarElement).val();
        var dateCar = $(dateCarElement).val();
        var quantityCar=$(quantityCarElement).val();
        var soChoCar = $(soChoCarElement).val();
        var concernCar = $(concernCarElement).val();
        var yearCar = $(yearCarElement).val();
        var seasionCar = seasionCarElement;

        priceCar.val('') ;
        totalPriceCar.val('');
        $('#btnInsertCar').attr('priceid', "-1");
        if (scheduleCar != -1 && soChoCar > 0 && dateCar != NaN && concernCar != -1 && yearCar != -1) {
            $.ajax({
                type: "POST",
                url: "/ajax_vmh.html",
                data: { "function": "PriceListByCar", "TypeId": typeId, "ScheduleCar": scheduleCar, "DateCar": dateCar, "SoChoCar": soChoCar, "ConcernCar": concernCar, "YearCar": yearCar, "SeasionCar": seasionCar },
                dataType: "json",
                success: function(data) {
                    if (data && data.isError == false) {
                        priceCar.val(data.PriceCar);
                        $('#btnInsertCar').attr('priceid', data.PriceId);
                        fncTotalPriceCar();
                    }
                }
            });
        }
    };
    $(scheduleCarElement).change(fncPriceCar);
    $(dateCarElement).change(fncPriceCar);
    $(soChoCarElement).change(fncPriceCar);
    $(concernCarElement).change(fncPriceCar);
    $(yearCarElement).change(fncPriceCar);
    //ham tinh tong
    var fncTotalPriceCar = function() {
        var vatCar = $(vatCarElement).val();
        var quantityCar = $(quantityCarElement).val();
        var priceCar = $(priceCarElement).val();
        if (vatCar > 0 && vatCar <= 100 && quantityCar > 0 && priceCar > 0) {
            var totalPriceCar = priceCar * quantityCar;
            var priceVat = parseInt(totalPriceCar) + parseInt(totalPriceCar * (vatCar / 100));
            $(totalPriceCarElement).val(priceVat);
        } else {
            if (quantityCar > 0 && priceCar >0) {
                $(totalPriceCarElement).val(priceCar * quantityCar);
            } else {
                $(totalPriceCarElement).val('');
            }
        }
    };
   //thay doi o luong xe
    $(quantityCarElement).keyup(fncTotalPriceCar);
    //vat
    $(vatCarElement).keyup(fncTotalPriceCar);
    //price car
    $(priceCarElement).keyup(fncTotalPriceCar);
    //click vao button them
    $('#btnInsertCar').live("click", function () {
        
        var typeId = 3;
        var tourId = parseInt(tourIdElement);
        var priceId = $(this).attr('priceid');
        var dateCar = $(dateCarElement).val();
        var quantityCar = $(quantityCarElement).val();
        var priceCar = $(priceCarElement).val();
        var vatCar = $(vatCarElement).val();
        if (vatCar > 0 && vatCar <= 100) { } else { vatCar = 0; }
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        var quantityAdults = parseInt(quantityAdultsElement);
        var quantityChild = parseInt(quantityChildElement);
        alert('QuantityAdults'+ quantityAdults + 'QuantityChild'+quantityChild);
        if (quantityCar > 0 && priceId > 0 ) {
            $.ajax({
                type: "POST",
                url: "/ajax_vmh.html",
                data: { "function": "InsertCar", "TypeId": typeId, "TourId": tourId, "TicketId": priceId, "PriceAdult": priceCar, "Date": dateCar, "QuantityCar": quantityCar, "CalculatorId": calculatorId, "ClassName": className,'VatCar':vatCar,'QuantityAdults':quantityAdults,'QuantityChild':quantityChild },
                dataType: "json",
                success: function (data) {

                    $(".ListCar").remove();
                    $(scheduleCarElement).val('-1');
                    $(quantityCarElement).val('');
                    $(soChoCarElement).val('-1');
                    $(dateCarElement).val('');
                    $(concernCarElement).val('-1');
                    $(yearCarElement).val('-1');
                    $(priceCarElement).val('');
                    $(totalPriceCarElement).val('');
                    $(vatCarElement).val('');
                    if (data && data.isError == false) {
                        $.each(data.array, function (index, item) {
                            $('#new-ticket-car')
                                .before($("<tr class='ListCar' id='ticket-car-"+item.CalculatorId+"'></tr>").html('<td></td>' +
                                    '<td >' + item.ScheduleName + '</td>' +
                                    '<td >' + item.DateBegin + '</td>' +
                                    '<td >' + item.SoCho + '</td>' +
                                    '<td >' + item.ConcernFullName + '</td>' +
                                    '<td >' + item.YearCar + '</td>' +
                                    '<td >' + item.PriceAdult + '</td>' +
                                    '<td >' + item.QuantityCar + '</td>' +
                                    '<td>' + item.VatCar + '</td>' +
                                    '<td >' + item.TotalPriceCar + '</td>' +
                                    '<td></td>' +
                                    '<td>' +
                                    '<input class="btnUpdateCar"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                    '<input class="btnDeleteCar"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                    '</td>'));

                        });
                        $('#TotalTicketCarPrices').html(data.TotalTicketCarPrices);
                        $('#TotalTicketCarPricesAdults').html(data.TotalTicketCarPricesAdults);
                        $('#TotalTicketCarPricesChild').html(data.TotalTravelGuidePricesChild);

                        $('#btnInsertCar').attr("calculatorid", "-1").attr('priceid', '-1').val("Thêm");
                        $('.btnUpdateCar').show();
                        $('#btnDeleteCar').hide();
                    }

                }
            });
        }
    });
    $('.btnUpdateCar').live("click", function () {
        var calculatorId = $(this).attr("calculatorid");

        $.ajax({
            type: "POST",
            url: "/ajax_vmh.html",
            data: { "function": "UpdateCar", "CalculatorId": calculatorId },
            dataType: "json",
            success: function (data) {
                $('#ticket-car-' + calculatorId).remove();
                $(scheduleCarElement).val(data.ScheduleId);
                $(dateCarElement).val(data.DateBegin);
                $(quantityCarElement).val(data.QuantityCar);
                $(soChoCarElement).val(data.SoCho);
                $(concernCarElement).val(data.ConcernId);
                $(yearCarElement).val(data.YearCar);
                $(priceCarElement).val(data.PriceCar);
                $(totalPriceCarElement).val(data.TotalPriceCar);
                $(vatCarElement).val(data.VatCar);
                $('#btnInsertCar').attr("calculatorid", calculatorId).attr('priceid',data.PriceId).val("Lưu");
                $('.btnUpdateCar').hide();
                $('#btnDeleteCar').attr('priceid',data.PriceId).show();
            }
        });
    });
    $('.btnDeleteCar').live("click", function () {

        var typeId = 3;
        var tourId = parseInt(tourIdElement);
        var priceId = $(this).attr('priceid');
        var dateCar = $(dateCarElement).val();
        var quantityCar = $(quantityCarElement).val();
        var priceCar = $(priceCarElement).val();
        var vatCar = $(vatCarElement).val();
        if (vatCar > 0 && vatCar <= 100) { } else { vatCar = 0; }
        var calculatorId = $(this).attr('calculatorid');
        var className = $(this).attr('class');
        var quantityAdults = parseInt(quantityAdultsElement);
        var quantityChild = parseInt(quantityChildElement);
        $.ajax({
            type: "POST",
            url: "/ajax_vmh.html",
            data: { "function": "InsertCar", "TypeId": typeId, "TourId": tourId, "TicketId": priceId, "PriceAdult": priceCar, "Date": dateCar, "QuantityCar": quantityCar, "CalculatorId": calculatorId, "ClassName": className, 'VatCar': vatCar, 'QuantityAdults': quantityAdults, 'QuantityChild': quantityChild },
            dataType: "json",
            success: function (data) {

                $(".ListCar").remove();
                $(scheduleCarElement).val('-1');
                $(quantityCarElement).val('');
                $(soChoCarElement).val('-1');
                $(dateCarElement).val('');
                $(concernCarElement).val('-1');
                $(yearCarElement).val('-1');
                $(priceCarElement).val('');
                $(totalPriceCarElement).val('');
                $(vatCarElement).val('');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $('#new-ticket-car')
                            .before($("<tr class='ListCar' id='ticket-car-" + item.CalculatorId + "'></tr>").html('<td></td>' +
                                '<td >' + item.ScheduleName + '</td>' +
                                '<td >' + item.DateBegin + '</td>' +
                                '<td >' + item.SoCho + '</td>' +
                                '<td >' + item.ConcernFullName + '</td>' +
                                '<td >' + item.YearCar + '</td>' +
                                '<td >' + item.PriceAdult + '</td>' +
                                '<td >' + item.QuantityCar + '</td>' +
                                '<td>' + item.VatCar + '</td>' +
                                '<td >' + item.TotalPriceCar + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateCar"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteCar"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));

                    });
                    $('#TotalTicketCarPrices').html(data.TotalTicketCarPrices);
                    $('#TotalTicketCarPricesAdults').html(data.TotalTicketCarPricesAdults);
                    $('#TotalTicketCarPricesChild').html(data.TotalTicketCarPricesChild);

                    $('#btnInsertCar').attr("calculatorid", "-1").attr('priceid', '-1').val("Thêm");
                    $('.btnUpdateCar').show();
                    $('#btnDeleteCar').hide();
                }

            }
        });
        
    });
};